package com.lam.common.pay.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.lam.common.utils.SpringUtils;
import com.lam.common.utils.StringUtils;

@Component
@Configurable
public class AlipayConfig {
	
	private static AlipayConfig alipayConfig;
	
	@Autowired
	private AlipayProperties normalProperties; 
	@Autowired
	private AlipayProperties sandboxProperties; 
	
	@Value("alipay.currentEvn")
	private String currentEvn;
	
	@Bean("normalProperties")
    @ConfigurationProperties(prefix = "alipay.normal")   
	public AlipayProperties createNormalProperties() {
		return new AlipayProperties();
	}
	
	@Bean("sandboxProperties")
    @ConfigurationProperties(prefix = "alipay.sandbox")   
	public AlipayProperties createSandboxProperties() {
		return new AlipayProperties();
	}
	
	public static AlipayConfig me() {
		if(alipayConfig == null) {
			alipayConfig = SpringUtils.getBean(AlipayConfig.class);
		}
		return alipayConfig;
	}
	
	private AlipayProperties getProperties() {
		if(StringUtils.equalsIgnoreCase(currentEvn, "normal")) {
			return normalProperties;
		}
		return sandboxProperties;
	}
	
	public String getCharset() {
		return getProperties().getCharset();
	}

	public String getSignType() {
		return getProperties().getSignType();
	}

	public String getFormat() {
		return getProperties().getFormat();
	}

	public String getAppId() {
		return getProperties().getAppId();
	}

	public String getMerchantPrivateKey() {
		return getProperties().getMerchantPrivateKey();
	}

	public String getAlipayPublicKey() {
		return getProperties().getAlipayPublicKey();
	}

	public String getNotifyUrl() {
		return getProperties().getNotifyUrl();
	}

	public String getReturnUrl() {
		return getProperties().getReturnUrl();
	}

	public String getGatewayUrl() {
		return getProperties().getGatewayUrl();
	}
	
}
